package com.ruoyi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.domain.TestUser;
import com.ruoyi.domain.param.StateParam;
import com.ruoyi.service.ITestUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 考试平台用户Controller
 */
@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/exam/user")
public class TestUserController extends BaseController
{
    @Autowired
    private ITestUserService testUserService;

    /**
     * TODO:启用禁用、提示必填、分页查询、#用户名模糊查询#
     */
    @ApiOperation("启用禁用参数修改功能")
    @PostMapping("/state")
    AjaxResult updateStatus(@RequestBody StateParam stateParam) {
            // 假设1表示启用，0表示禁用
            if (testUserService.updateStatus(stateParam)==1){
                return AjaxResult.success("State changed successfully"); // 返回成功消息给前端
            }else {
                return AjaxResult.error("Failed to change function state"); // 返回错误信息给前端
            }
    }

    /**
     * 分页、根据用户名模糊查询
     */
    @ApiOperation("学生列表查询")
    @GetMapping("/student")
    public AjaxResult getStudents(String userName) {
        startPage();
        QueryWrapper<TestUser> wrapper = new QueryWrapper<TestUser>();
        if (userName != null) {
            wrapper.eq("remark","学生").like("user_name", userName);
        }else {
            wrapper.eq("remark","学生");
        }
        List<TestUser> list = testUserService.getStudents(userName, wrapper);
        return AjaxResult.success(list);
    }

    @ApiOperation("教师列表查询")
    @GetMapping("/teacher")
    AjaxResult getTeachers(String userName) {
        startPage();
        QueryWrapper<TestUser> wrapper = new QueryWrapper<TestUser>();
        if (userName != null) {
            wrapper.eq("remark","教师").like("user_name", userName);
        }else {
            wrapper.eq("remark","教师");
        }
        List<TestUser> list = testUserService.getTeachers(userName, wrapper);
        return AjaxResult.success(list);
    }

    @ApiOperation("管理员列表查询")
    @GetMapping("/administrators")
    public AjaxResult getAdministrators(String userName) {
        startPage();
        QueryWrapper<TestUser> wrapper = new QueryWrapper<TestUser>();
        if (userName != null) {
            wrapper.eq("remark","管理员").like("user_name", userName);
        }else {
            wrapper.eq("remark","管理员");
        }
        List<TestUser> list = testUserService.getAdministrators(userName, wrapper);
        return AjaxResult.success(list);
    }
    /**
     * 获取考试平台某用户详细信息
     */
    @ApiOperation("获取考试平台某用户详细信息")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return AjaxResult.success(testUserService.selectTestUserByUserId(userId));
    }

    /**
     * 新增考试平台用户
     */
    @ApiOperation("新增考试平台用户")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody TestUser testUser)
    {
        QueryWrapper<TestUser> student = new QueryWrapper<TestUser>();
        student.eq("remark","学生").like("user_name", testUser.getUserName());;
        QueryWrapper<TestUser> teacher = new QueryWrapper<TestUser>();
        teacher.eq("remark","教师").like("user_name", testUser.getUserName());;
        QueryWrapper<TestUser> administrator = new QueryWrapper<TestUser>();
        administrator.eq("remark","管理员").like("user_name", testUser.getUserName());;
        if(testUserService.getStudents(testUser.getUserName(), student).isEmpty()
                &testUserService.getTeachers(testUser.getUserName(),teacher).isEmpty()
                &testUserService.getAdministrators(testUser.getUserName(),administrator).isEmpty())
        {
            return toAjax(testUserService.insertTestUser(testUser));
        }else {
            return AjaxResult.error("要添加的该用户已存在");
        }
    }

    /**
     * 修改考试平台用户
     */
    @ApiOperation("修改考试平台用户")
    @PutMapping
    public AjaxResult edit(@RequestBody TestUser testUser)
    {
        return toAjax(testUserService.updateTestUser(testUser));
    }

    /**
     * 删除考试平台用户
     */
    @ApiOperation("删除考试平台用户")
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(testUserService.deleteTestUserByUserIds(userIds));
    }
}
